<?php

namespace app\api\controller;

use Endroid\QrCode\QrCode;
use queue\Queue;
use think\Log;
use tool\Res;

class Group extends Base
{
    public function ewm()
    {
        $group_code=i("group_code","请输入群编号");
        $app=$this->getwxapp();
        $result = $app->qrcode->forever(intval($group_code)+50000);
        $url=$result['url'];
        $qrCode = new QrCode($url);
        $qrCode->setSize(220);
        header('Content-Type: '.$qrCode->getContentType());
        return response($qrCode->writeString())->contentType('Content-Type: image/png');

    }

    public function get_extension(){
        $group_code=input("group_code");
        $beijing = "http://res.xckj789.com/weiqun/article/5be29cf1c9f01.jpg?x-oss-process=image/resize,m_fill,h_650,w_430";
        $beijing = imagecreatefromjpeg($beijing);
        $ewm=tempnam("/tmp","head");
        file_put_contents($ewm,file_get_contents("http://server.xckj789.com/api/group/ewm?group_code={$group_code}"));
        $logourl = imagecreatefrompng($ewm);
        imagecopymerge($beijing,$logourl, 100,320,0,0,240,240,100);
        imagepng($beijing);
        return response(null)->contentType('Content-Type: image/png');
    }

   public function memberlists(){
       $group=$this->getgroup();
        $lists=model("user")->where("group_code",$group['code'])->order("id desc")->select();
        return s($lists);
   }
   public function nextlists(){
      $group=$this->getgroup();
      $list=model("group")->where("up_code",$group['code'])->select();
      return s($list);
   }


   public function sublists(){
       $code=i("code");
       $list=model("group")->where("up_code",$code)->select();
       return s($list);
   }

   public function join(){
       $user=$this->getuser(false);
       if(empty($user['group_code'])){
           e("您没有推荐人，不能申请成为群主");
       }
       $user_id=$user['id'];
       $group=model("group")->where("user_id",$user_id)->find();
       if(!empty($group)){
           e("您已入住，请不要重复入住");
       }
       $info=[];
       $info['name']=i("name","请输入群名");
       $info['personal_name']=i("personal_name","请输入个人姓名");
       $info['tel']=i("tel","请输入电话");
       $info['area']=i("area","请输入所在地");
       $isnext=false;
       if(!empty($user['group_code'])){
           $info['up_code']=$user['group_code'];
           $isnext=true;

       }

       $info['user_id']=$user_id;
       model("group")->allowField(true)->save($info);
       $code=model("group")->getLastInsID();
       integral($code,100);
       $this->setgroup_next_number($user['group_code']);
       return s();
   }
    public function edit(){
        $group=$this->getgroup();
        $info=[];
        $info['name']=i("name","请输入群名");
        $info['personal_name']=i("personal_name","请输入个人姓名");
        $info['tel']=i("tel","请输入电话");
        $info['area']=i("area","请输入所在地");
        model("group")->allowField(true)->save($info,['code'=>$group['code']]);
        return s();
    }

   public function user_get(){
       $group=$this->getgroup();
       $leval=$this->getlevel($group['code']);
       $group['leval_name']="";
       if($leval!==false){
           $group['leval_name']=$leval['name'];
       }
       $group['up_name']="无";
       if(!empty($group['up_code'])){
           $up_group=model("group")->where("code",$group['up_code'])->find();
           $group['up_name']=$up_group['personal_name']."-".$up_group['tel'];
       }
       return s($group);
   }
   private function getgroup(){
       $user_id=$this->getuser();
       $group=model("group")->where("user_id",$user_id)->find();
       if(empty($group)){
           e("您还没加入群");
       }

       return $group;
   }

}
